﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Stote.UControls
{
    public partial class RoundedPanel : Panel
    {
        public RoundedPanel()
        {
            InitializeComponent();
            // 采用双缓冲技术绘制
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.Selectable | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);
        }

        private bool isRadius = false;
        [Description("是否圆角")]
        public bool IsRadius
        {
            get { return isRadius; }
            set { isRadius = value; Refresh(); }
        }

        private int cornerRadius = 24;
        [Description("圆角角度")]
        public int ConerRadius
        {
            get { return cornerRadius; }
            set { cornerRadius = Math.Max(value, 1); Refresh(); }
        }

        private bool isShowRect = false;
        [Description("是否显示边框")]
        public bool IsShowRect
        {
            get { return isShowRect; }
            set { isShowRect = value; Refresh(); }
        }

        private Color rectColor = Color.FromArgb(220, 220, 220);
        [Description("边框颜色")]
        public Color RectColor
        {
            get { return rectColor; }
            set { rectColor = value; Refresh(); }
        }

        private int rectWidth = 1;

        [Description("边框宽度")]
        public int RectWidth
        {
            get { return rectWidth; }
            set { rectWidth = value; Refresh(); }
        }

        private Color fillColor = Color.Transparent;
        [Description("填充颜色")]
        public Color FillColor
        {
            get { return fillColor; }
            set { fillColor = value; Refresh(); }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (!Visible) return;

            GraphicsPath graphicsPath = new GraphicsPath();
            Rectangle rect = ClientRectangle;
            if (isRadius)
            {
                graphicsPath.AddArc(rectWidth / 2, rectWidth / 2, cornerRadius, cornerRadius, 180f, 90f);
                graphicsPath.AddArc(rect.Width -  2 * rectWidth - 1 - cornerRadius, rectWidth / 2, cornerRadius, cornerRadius, 270f, 90f);
                graphicsPath.AddArc(rect.Width - 2 * rectWidth - 1 - cornerRadius, rect.Height - 2 * rectWidth - 1 - cornerRadius, cornerRadius, cornerRadius, 0f, 90f);
                graphicsPath.AddArc(rectWidth / 2, rect.Height - 2 * rectWidth - 1 - cornerRadius, cornerRadius, cornerRadius, 90f, 90f);
                graphicsPath.CloseFigure();
            }
            else
            {
                if (isShowRect)
                    rect = new Rectangle(rectWidth / 2, rectWidth / 2, Width - 2 * rectWidth - 1, Height - 2 * rectWidth - 1);
                graphicsPath.AddRectangle(rect);
            }

            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;  //使绘图质量最高，即消除锯齿
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.FillPath(new SolidBrush(fillColor), graphicsPath);
            if (isShowRect)
            {
                g.DrawPath(new Pen(rectColor, rectWidth), graphicsPath);
            }
        }
    }
}
